package com.fast.generator.custom;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;


@Data
public class AutoGeneratorCustom extends AutoGenerator {

    private static final Logger logger = LoggerFactory.getLogger(AutoGeneratorCustom.class);

    private GlobalConfigCustom globalConfigCustom;

    @Override
    public void execute() {
        DataSourceConfig dataSource = getDataSource();
        StrategyConfig strategy = getStrategy();
        PackageConfig packageInfo = getPackageInfo();
        TemplateConfig template = getTemplate();
        GlobalConfigCustom globalConfig = getGlobalConfigCustom();
        AbstractTemplateEngine templateEngine = getTemplateEngine();


        logger.debug("==========================准备生成文件...==========================");
        // 初始化配置
        if (null == config) {
            config = new ConfigBuilder(packageInfo, dataSource, strategy, template, globalConfig);
            if (null != injectionConfig) {
                injectionConfig.setConfig(config);
            }
            if (null != globalConfig.getOutputControllerDir()) {

                //修改控制器生成包名

                String basePath = packageInfo.getController() + ConstValCustom.DOT + globalConfig.getControllerModelName() + ConstValCustom.DOT;
                config.getPackageInfo().put(ConstVal.CONTROLLER, basePath + ConstValCustom.CONTROLLER);
                //增加控制器入参包名
                config.getPackageInfo().put(ConstValCustom.PARAM_UPPER, basePath + ConstValCustom.PARAM);
                //增加控制器出参包名
                config.getPackageInfo().put(ConstValCustom.RESULT_UPPER, basePath + ConstValCustom.RESULT);

                config.getPackageInfo().put(ConstValCustom.CONTROLLER_MODULE, globalConfigCustom.getControllerModelName());

                //增加控制器入参路径
                config.getPathInfo().put(ConstValCustom.PARAM_UPPER, joinPath(
                        globalConfigCustom.getOutputControllerDir(),
                        config.getPackageInfo().get(ConstValCustom.PARAM_UPPER)
                ));

                //增加控制器出参路径
                config.getPathInfo().put(ConstValCustom.RESULT_UPPER, joinPath(
                        globalConfigCustom.getOutputControllerDir(),
                        config.getPackageInfo().get(ConstValCustom.RESULT_UPPER)
                ));


                //修改控制器的文件生成路径
                config.getPathInfo().put(
                        ConstVal.CONTROLLER_PATH,
                        joinPath(
                                globalConfigCustom.getOutputControllerDir(),
                                config.getPackageInfo().get(ConstVal.CONTROLLER)
                        )
                );


            }


        }
        if (null == templateEngine) {
            // 为了兼容之前逻辑，采用 Velocity 引擎 【 默认 】
            VelocityTemplateEngineCustom templateEngines = new VelocityTemplateEngineCustom();
            templateEngines.init(this.pretreatmentConfigBuilder(config), globalConfigCustom).mkdirs().batchOutput().open();
        }
        // 模板引擎初始化执行文件输出


        logger.debug("==========================文件生成完成！！！==========================");
    }


    private String joinPath(String parentDir, String packageName) {
        if (StringUtils.isBlank(parentDir)) {
            parentDir = System.getProperty(ConstVal.JAVA_TMPDIR);
        }
        if (!StringUtils.endsWith(parentDir, File.separator)) {
            parentDir += File.separator;
        }
        packageName = packageName.replaceAll("\\.", StringPool.BACK_SLASH + File.separator);
        return parentDir + packageName;
    }


}
